package maps.editor;

import grid.Grid;
import grid.GridTypeException;
import grid.HexGrid;
import grid.Node;
import grid.SquareGrid;
import grid.TriangleGrid;
import gui2Dengine.GridPanel;
import gui2Dengine.HexGridPanel;
import gui2Dengine.SquareGridPanel;
import gui2Dengine.TriangleGridPanel;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.io.FileNotFoundException;
import java.net.URISyntaxException;

import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import tile.Tile;

public class Test {
    
    static class MPopup extends JPopupMenu implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            this.setVisible(false);
        }
        
    }
    
    static class MFrame extends JFrame implements MouseListener, MouseMotionListener {

        GridPanel p;
        
        private MFrame(GridPanel panel) {
            p=panel;
        }

        public void mouseClicked(MouseEvent e) {
            if(e.getButton()==MouseEvent.BUTTON3) {
                MPopup popup = new MPopup();
                Node node = p.getNode(e.getPoint());
                Iterator<Tile> itr = node.returnTiles().iterator();
                while (itr.hasNext()) {
                    Tile tile = itr.next();
                    popup.add(new JMenuItem(tile.getString()));
                }
                popup.setLocation(e.getPoint());
                popup.setVisible(true);
                javax.swing.Timer timer = new javax.swing.Timer(4000, popup);
                timer.setRepeats(false);
                timer.start();
            }
        }

        public void mousePressed(MouseEvent e) {
            System.out.println(p.getNode(e.getLocationOnScreen()));
        }

        public void mouseReleased(MouseEvent e) {
            //throw new UnsupportedOperationException("Not supported yet.");
        }

        public void mouseEntered(MouseEvent e) {
            //throw new UnsupportedOperationException("Not supported yet.");
        }

        public void mouseExited(MouseEvent e) {
            //throw new UnsupportedOperationException("Not supported yet.");
        }

        public void mouseDragged(MouseEvent e) {
            //throw new UnsupportedOperationException("Not supported yet.");
        }

        public void mouseMoved(MouseEvent e) {
            //throw new UnsupportedOperationException("Not supported yet.");
        }
        
    }

	/**
	 * @param args
	 */
	public static void main(String[] args) {
            System.out.println(System.getProperty("user.dir"));
		try {
			MapIO mio = new MapIO("maps/editor/newmap.xml");
			GridPanel panel = null;
			Grid grid = null;
			switch(mio.returnGridType()) {
			case HEXGRID:
			{
				grid = new HexGrid(mio.xsize, mio.ysize);
				panel = new HexGridPanel((HexGrid) grid, 1f, 800, 600, 10, 20, 0, 0);
				break;
			}
			case SQRGRID:
			{
				grid = new SquareGrid(mio.xsize, mio.ysize);
				panel = new SquareGridPanel((SquareGrid) grid, 1f, 800, 600, 10, 20, 0, 0);
				break;
			}
			case TRIGRID:
			{
				grid = new TriangleGrid(mio.xsize, mio.ysize);
				panel = new TriangleGridPanel((TriangleGrid) grid, 1f, 800, 600, 10, 20, 0, 0);
				break;
			}
			}
			mio.initializeGrid(grid);
                        
			MFrame mainframe = new MFrame(panel);
			mainframe.setSize(800, 600);
			mainframe.add(panel);
                        panel.addMouseListener(mainframe);
			mainframe.setVisible(true);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (GridTypeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (URISyntaxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
